<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="表名称" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表描述" prop="tableComment">
        <el-input
          v-model="queryParams.tableComment"
          placeholder="请输入表描述"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Download"
          @click="handleGenTable"
          v-hasPermi="['tool:gen:code']"
        >生成代码</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleEdit"
          v-hasPermi="['tool:gen:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tool:gen:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleImportTable"
          v-hasPermi="['tool:gen:import']"
        >导入表</el-button>
      </el-col>
      <right-toolbar :showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="50" type="index" />
      <el-table-column label="表名称" align="center" prop="tableName" :show-overflow-tooltip="true" />
      <el-table-column label="表描述" align="center" prop="tableComment" :show-overflow-tooltip="true" />
      <el-table-column label="实体类" align="center" prop="className" :show-overflow-tooltip="true" />
      <el-table-column label="作者" align="center" prop="functionAuthor" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="text"
            icon="View"
            @click="handlePreview(scope.row)"
            v-hasPermi="['tool:gen:preview']"
          >预览</el-button>
          <el-button
            type="text"
            icon="Edit"
            @click="handleEdit(scope.row,scope.index)"
            v-hasPermi="['tool:gen:edit']"
          >修改</el-button>
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row,scope.index)"
            v-hasPermi="['tool:gen:remove']"
          >删除</el-button>
          <el-button
            type="text"
            icon="Download"
            @click="handleGenTable(scope.row,scope.index)"
            v-hasPermi="['tool:gen:code']"
          >生成代码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page="queryParams.pageNum"
      :limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 预览界面 -->
    <el-dialog title="代码预览" v-model="open" width="80%" append-to-body>
      <el-tabs v-model="activeName">
        <el-tab-pane
          v-for="(value, key) in preview"
          :label="key.substring(key.lastIndexOf('/') + 1)"
          :name="key"
          :key="key"
        >
          <pre><code class="language-{{ getLanguage(key) }}">{{ value }}</code></pre>
        </el-tab-pane>
      </el-tabs>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { listTable, getGenTable, delTable, previewTable, genCode, synchDb } from "@/api/tool/gen";

const { proxy } = getCurrentInstance();

const tableList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const activeName = ref("");
const preview = ref({});

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    tableName: undefined,
    tableComment: undefined
  }
});

const { queryParams, form } = toRefs(data);

/** 查询表集合 */
function getList() {
  loading.value = true;
  listTable(queryParams.value).then(response => {
    tableList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.tableId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 预览按钮操作 */
function handlePreview(row) {
  previewTable(row.tableId).then(response => {
    preview.value = response.data;
    open.value = true;
    activeName.value = Object.keys(response.data)[0];
  });
}

/** 修改按钮操作 */
function handleEdit(row, index) {
  const tableId = row.tableId || ids.value[index];
  proxy.$router.push({ path: '/tool/gen-edit/index/' + tableId });
}

/** 删除按钮操作 */
function handleDelete(row, index) {
  const tableIds = row.tableId || ids.value[index];
  proxy.$modal.confirm('是否确认删除表编号为"' + tableIds + '"的数据项？').then(function() {
    return delTable(tableIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 生成代码操作 */
function handleGenTable(row, index) {
  const tableIds = row.tableId || ids.value[index];
  if (tableIds == "") {
    proxy.$modal.msgError("请选择要生成的数据");
    return;
  }
  genCode(tableIds).then(response => {
    proxy.$modal.msgSuccess("成功生成到自定义路径");
  });
}

/** 导入表操作 */
function handleImportTable() {
  proxy.$modal.msgInfo("请通过数据库管理工具导入表结构");
}

/** 获取语言类型 */
function getLanguage(path) {
  if (path.indexOf('.vue') !== -1) return 'html';
  if (path.indexOf('.java') !== -1) return 'java';
  if (path.indexOf('.xml') !== -1) return 'xml';
  if (path.indexOf('.sql') !== -1) return 'sql';
  return 'text';
}

getList();
</script>